package cn.wetcland.urban.admin.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.wetcland.urban.admin.dto.OrderUpdateDTO;
import cn.wetcland.urban.admin.mapper.UtOrderMapper;
import cn.wetcland.urban.admin.service.UtOrderService;
import cn.wetcland.urban.admin.vo.DashboardVO;
import cn.wetcland.urban.admin.vo.LastOrderItemVO;
import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.generator.domain.UtCustomer;
import cn.wetcland.urban.generator.domain.UtOrder;
import cn.wetcland.urban.generator.dto.BaseConditionSearchDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author Wetcland
* @description 针对表【ut_order】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtOrderServiceImpl extends ServiceImpl<UtOrderMapper, UtOrder> implements UtOrderService {

    @Resource
    private UtOrderMapper orderMapper;

    @Override
    public Page<UtOrder> orderList(BaseConditionSearchDTO searchDTO) {
        Page<UtOrder> orderPage = new Page<>(searchDTO.getCurPage(), searchDTO.getPageSize());
        return orderMapper.selectOrderListPage(orderPage, searchDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderUpdate(OrderUpdateDTO updateDTO) {
        UtOrder utOrder = orderMapper.selectById(updateDTO.getOrderId());
        MyAssert.notNull(utOrder, "订单不存在");
        utOrder.setOrderStatus(updateDTO.getOrderStatus());
        utOrder.setFollowerNum(updateDTO.getFollowerNum());
        utOrder.setPrice(updateDTO.getPrice());
        orderMapper.updateById(utOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void orderDelete(Long orderId) {
        orderMapper.deleteById(orderId);
    }

    @Override
    public DashboardVO dashboardDetails() {
        DashboardVO dashboardVO = orderMapper.selectDashboardDetails();
        List<LastOrderItemVO> lastOrderItems = orderMapper.selectLastOrderNum();
        Map<String, Integer> map = new HashMap<>();
        for (LastOrderItemVO lastOrderItem : lastOrderItems) {
            map.put(lastOrderItem.getTime(), lastOrderItem.getCount());
        }
        Date date = new Date();
        DateTime dateTime = DateUtil.offsetDay(date, -30);
        Calendar calendar = dateTime.toCalendar();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        for (int i = 0; i < 30; i++) {
            String format = simpleDateFormat.format(calendar.getTime());
            boolean b = map.containsKey(format);
            if (b) {
                calendar.add(Calendar.DAY_OF_YEAR, 1);
                continue;
            } else {
                map.put(format, 0);
            }
            calendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        List<Map.Entry<String, Integer>> entryList1 = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(entryList1, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        List<LastOrderItemVO> lastOrderItemVOS = new ArrayList<>();
        for (Map.Entry<String, Integer> stringIntegerEntry : entryList1) {
            LastOrderItemVO lastOrderItemVO = new LastOrderItemVO();
            lastOrderItemVO.setTime(stringIntegerEntry.getKey());
            lastOrderItemVO.setCount(stringIntegerEntry.getValue());
            lastOrderItemVOS.add(lastOrderItemVO);
        }
        dashboardVO.setLastOrderItems(lastOrderItemVOS);
        return dashboardVO;
    }
}




